<!-- :class="['navItem', navActive === item.value ? 'actives' : '']" -->
<template>
  <div class="anchor-wrap">
    <div class="nav-wrap">
      <div class="navBox">
        <div v-for="item in navList"
             v-show="item.value"
             :key="item.value"
             :class="['navItem']"
             @click="goAnchor(item.id,item.value)">
          {{ item.label }}
        </div>
      </div>
    </div>
    <div>
      <el-col :span="spanRow">
        <div name="table"
             class="box-col">
          <slot></slot>
        </div>
      </el-col>
    </div>
  </div>
</template>

<script>

export default {
  name: 'Anchors',
  props: {
    navList: {
      type: Array,
      default: () => []
    },
    spanRow: {
      type: [String, Number],
      default: 16
    },
    activeIndex: {
      type: Number,
      default: null
    }
  },
  data () {
    return {
      navClickFlag: false,
      scrollWrap: "box",
      navActive: 1,
      // 各锚点的高度
      offTopLs: [],
    }
  },
  mounted () {
    // 监听滚动
    window.addEventListener("scroll", this.handleScroll, true);
    this.$nextTick(() => {
      // 计算锚点的高度
      this.getoffTop();
    });

  },
  beforeDestroy () {
    // 销毁监听
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    // 计算锚点的高度
    getoffTop () {
      let offTop = [];
      for (let index = 0; index < this.navList.length; index++) {
        const element = this.navList[index];
        if (document.getElementById(element.id)) {
          offTop.push(document.getElementById(element.id).offsetTop);
        }
      }
      this.offTopLs = offTop;
    },
    handleScroll () {
      if (!this.navClickFlag && document.getElementById(this.scrollWrap)) {
        let scTop = document.getElementById(this.scrollWrap).scrollTop;
        for (let index = 0; index < this.offTopLs.length; index++) {
          const element = this.offTopLs[index];
          let offsetTop = document.getElementById(this.scrollWrap).offsetTop;
          if (scTop + offsetTop >= element) {
            this.navActive = index + 1;
          }
        }
      }
    },
    // 锚点导航
    goAnchor (keyId, val) {
      this.navClickFlag = true;
      this.navActive = val;
      // document.getElementById(keyId).scrollIntoView(true);
      let el = document.getElementById(keyId)
      el.scrollIntoView({
        behavior: 'smooth', // 平滑过渡
        block: 'start',
        inline: 'start'
      })
      setTimeout(() => {
        this.navClickFlag = false;
      }, 2000);
    }
  }
}
</script>

<style lang="scss" scoped>
.anchor-wrap {
  // padding-top: 20px;
}
.box-col {
  height: calc(100vh - 236px);
  // overflow-y: auto;
  overflow-x: hidden;
  overflow-y: scroll;
  box-sizing: border-box;
  scroll-behavior: smooth;
}
.box-col::-webkit-scrollbar {
  display: none;
}
.box-card {
  height: 200px;
  margin-bottom: 10px;
}
.nav-wrap {
  display: flex;
  padding: 10px 20px 20px 20px;
  // justify-content: flex-end;
}
.navBox {
  // width: 200px;
  // height: 150px;
  // overflow-y: auto;
  display: flex;
  // margin-bottom: 20px;
  // margin-left: 10px;
  // border-left: 2px solid rgba(0, 0, 0, 0.09);
  // margin-top: 20px;
}
.navItem {
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #000000;
  padding: 8px;
  margin-left: -2px;
  cursor: pointer;
  border-left: 2px solid transparent;
  padding: 0 20px;
  height: 30px;
  line-height: 30px;
  border: 1px solid var(--prev-color-primary);
  margin-right: 12px;
  border-radius: 7px;
}
.navItem:hover {
  color: var(--prev-color-primary);
  background-color: rgba(235, 102, 43, 0.2);
}
.navItem.actives {
  background-color: var(--prev-color-primary);
  color: var(--prev-bg-white);
}
</style>